<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * 文件搜索
 * @author xuelange
 *
 */
class F_search extends MY_Controller {
	
	// 每页显示条数
	private $page = 100;
	private $curPage = 1;
	private $totalPage = 0;
	
	
	public function __construct() {
		parent::__construct ();
		log_message ( 'debug', "F_search  Class Initialized" );
	}
	
	public function index() {
		$page = $this->input->get ( "page" );
		$this->curPage = ( int ) $page;
		log_message ( 'debug', 'F_search index page=.' . $page );
		
		$keyword = trim($this->input->get("keyword"));
		if(strlen($keyword) == 0){
			log_message ( 'debug', 'F_search index keyword is null.' );
		}
		if(strlen ($keyword) > 300){
			log_message ( 'debug', 'F_search index keyword is too long.' );
			common::prf_json ( false, null, null );
			return true;
		}
		
		$record = $this->curPage;
		
		if ($record > 0) {
			$record = ($this->curPage) * $this->page - $this->page;
			log_message ( 'debug', 'F_search toPage $record=.' . $record );
			log_message ( 'debug', 'F_search toPage $this->curPage=' . $this->curPage );
			log_message ( 'debug', 'F_search toPage $this->page=' . $this->page );
		}
		if ($record < 0) {
			log_message ( 'debug', 'F_search toPage $record<0 set $record=' . $record );
			$record = 0;
		}
		
		// 获取某个用户所有文件列表
		$this->db->where("deleted","0");
		$this->db->where("uid",$this->uid);
		$this->db->select ( 'file.*' );
		$this->db->from ( 'file' );
		if (strlen($keyword) > 0) {
			$this->db->like ( 'file.name', $keyword );
		}
		$this->db->limit ( $this->page, $record );
		$this->db->order_by ( "creation_ts", "desc" );
		$this->db->select("file.*");
		$query = $this->db->get ();
		$filelist = $query->result_array ();
		
		if ($this->db->affected_rows () < 1) {
			log_message ( 'error', 'F_search list is null.' );
		}
		
		
		$this->db->where("uid",$this->uid);
		$this->db->where("deleted","0");
		if (strlen($keyword) > 0) {
			$this->db->like ( 'file.name', $keyword );
		}
		$fileTotal = $this->db->count_all_results('file');//总行数;
		
		$totalPage = ceil ( $fileTotal / $this->page );
		
		if ($totalPage == 1)
			$totalPage = 0;
		$newList = array ();
		foreach ( $filelist as $k => $file ) {
			$fileArr = array();
			$fileArr['type'] = Common::get_file_type ( $file ['name'] );
			$fileArr['name'] = $file ['name'] ;
			$fileArr ['index'] = ($record + 1 + $k);
			$fileArr['size'] = Common::get_file_size ( $file ['size'] );
			$fileArr['created_ts'] = date ( 'Y-m-d H:i', $file ['creation_ts']/1000 );
			$fileArr['uuid'] = $file ['uuid'];
			$fileArr['res_id'] =  $this->common->create_uuid();
			if($file ['access_denied'] == 1){
				$fileArr ['access_denied'] = $file ['access_denied'] ;
			}
			$newList[$k] = (object) $fileArr;
		}
		$paramArr = array(
			"totalPage" => $totalPage,
			"fileTotal" => $fileTotal,
			"page"		=> $this->page,
			"keyword"	=> $keyword
		);
		common::prf_json ( TRUE, $newList, $paramArr );
		return true;
	}
}
